Component({
  /**
   * 组件的属性列表
   */
  properties: {
    campGoodsStatus: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    campGoodsStatus: false,
    addList: [], //整体数据
    fileList: [],
    goodsName: "",
    memberPrice: "",
    goodsPrice: "",
    goodsImage: ""
  },
  lifetimes: {
    ready() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    afterRead(event) {
      var that = this;
      const { file } = event.detail;
      const userInfo = wx.getStorageSync('userInfo') || null;
      // console.log(userInfo,"######");
      const token = userInfo.token
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: 'http://127.0.0.1:8080/common/upload', // 仅为示例，非真实的接口地址
        filePath: file.url,
        name: 'file',
        formData: { user: 'test' },

        header: {
          'Authorization': `Bearer ${token}`,
          'content-type': 'application/json',
        },
        success(res) {
          // 上传完成需要更新 fileList
          const { fileList = [] } = that.data;
          var resData = JSON.parse(res.data)
          console.log("goodsImage res", res)
          console.log("goodsImage resData", resData)
          that.setData({
            goodsImage: resData.fileName,
          })
          fileList.push({ ...file, url: res.data });
          that.setData({ fileList });
          console.log("fileList", that.data.fileList)
        },
      });
    },
    //重置数据
    resetData() {
      this.setData({
        // conditionList: result
        goodsName: "",
        memberPrice: "",
        goodsPrice: "",
        introduce: "",
        goodsImage: "",
        fileList: []
      })
    },
    //添加虚拟数据
    addData(e) {
      const object = {
        goodsName: e.detail.value.goodsName,
        memberPrice: Number(e.detail.value.memberPrice),
        goodsPrice: Number(e.detail.value.goodsPrice),
        goodsImage: this.data.goodsImage
      }
      console.log(object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        this.triggerEvent("sendData", object)
        this.resetData();
        this.onClose();
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }

      // console.log(instance.globalData.campData)
      console.log(this.data.addList);
    },
    onClose() {
      this.resetData()
      this.setData({
        campGoodsStatus: false
      })
    },
  }
})